<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
class WebConfigController extends AdminController {
	public function index(){
		$where['web_type'] = array('neq','0');
		$arr=M('WebConfig')->where($where)->select();
		foreach ($arr as $k=>$v){
			$config[$v['web_key']]=$v;
		}
		$this -> assign('webconfig',$config);
		$this ->display();
	}
	public function add(){
         $key   = I('key');
         $value = I('value');
         $text  = I('text');
         $type  = I('type');
         if(M('WebConfig')->where(array('web_key'=>$key))->find()){
         	$this -> error('key值已存在');
         }
         if(empty($text)){
         	$this -> error('说明不能为空');
         }
         $data['web_key']   = $key;
         $data['web_value'] = $value;
         $data['web_text']  = $text;
         $data['web_type']  = $type;
         if(!M('WebConfig')->add($data)){
         	$this -> error('系统繁忙');
         }
         $this -> success('添加成功');
	}
	public function edit(){
		$data = $this ->uploadAll();
		$configkey = M('WebConfig')->field('web_key')->select();
		foreach($configkey as $k=>$v){
			$arr[$v['web_key']] = I($v['web_key']);
		}
		if($data['status'] == 1){
			foreach ($data['data'] as $key => $value) {
			  $arr[$value['name']] = $value['url'];
		    }
		}
		foreach($arr as $kk=>$vv){
			$re[] = M('WebConfig')->where(array('web_key' => $kk))->setField('web_value',$vv);
			//$re[] = M('Config')->where(C("DB_PREFIX")."config.key='$kk'")->setField('value',$vv);
		}
		if(!$re){
			$this -> error('修改失败');
		}
		$this -> success('修改成功');
	}
}